package com.gitee.melin.bee.core.jdbc.dialect.impl;

import com.gitee.melin.bee.core.jdbc.dialect.AbstractJdbcDialect;
import com.gitee.melin.bee.core.jdbc.enums.DataSourceType;
import com.gitee.melin.bee.core.jdbc.relational.BeeTableType;
import com.gitee.melin.bee.core.jdbc.relational.ConnectionInfo;
import java.sql.Connection;
import java.sql.Driver;
import org.apache.commons.lang3.StringUtils;

public class InceptorJdbcDialect extends AbstractJdbcDialect {

    @Override
    public DataSourceType getDataSourceType() {
        return DataSourceType.INCEPTOR;
    }

    public InceptorJdbcDialect(ConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    public InceptorJdbcDialect(Connection connection) {
        super(connection);
    }

    @Override
    protected Driver getDriver() {
        return loadDriver(DataSourceType.INCEPTOR.getDriverClass());
    }

    @Override
    protected String getDDLColumnName(BeeTableType type) {
        throw new IllegalArgumentException("未定义 DDLColumnName");
    }

    @Override
    protected String getJdbcUrl() {
        String jdbcUrl = this.connectionInfo.getUrl();
        if (StringUtils.isNotBlank(this.connectionInfo.getHivePrincipal())
                || StringUtils.isNotBlank(this.connectionInfo.getKrb5File())
                || StringUtils.isNotBlank(this.connectionInfo.getKeytabFile())) { // kerberos 认证

            if (!StringUtils.contains(jdbcUrl, ";principal=")) {
                jdbcUrl = jdbcUrl + ";principal=" + this.connectionInfo.getHivePrincipal();
            }

            jdbcUrl = jdbcUrl + ";authentication=kerberos";
            jdbcUrl = jdbcUrl + ";kuser=" + this.connectionInfo.getPrincipal();
            jdbcUrl = jdbcUrl + ";keytab=" + this.connectionInfo.getKeytabFile();
            jdbcUrl = jdbcUrl + ";krb5conf=" + this.connectionInfo.getKrb5File();
        }

        return jdbcUrl;
    }
}
